'use strict';

webappApp.directive('uploadGallery', ['fileSupport', function(fileSupport) {
  return {
    templateUrl: 'tmpl/uploadGallery.html',
    restrict: 'E',
    scope: {
      files: '=',
      onchange: '=',
      progress: '=',
      clear: '&'
    },
    controller: function ($scope) {
      $scope.cancel = function () {
        $scope.clearInput();
        if ($scope.clear) {
          $scope.clear();
        }
      };

      $scope.size = Object.size;

      $scope.fileSupport = fileSupport;
    },
    link: function postLink(scope, element, attrs) {
      scope.clearInput = function () {
        element.find('input[type=file]')[0].value = '';
      };
    }
  };
}])
.factory('fileSupport', ['$window', function ($window) {
  return !(angular.isUndefined($window.File) ||
           angular.isUndefined($window.FileReader) ||
           angular.isUndefined($window.FileList));
  }
]);
